package com.gengzp.system.controller;

import com.gengzp.common.annotations.OperationLogIgnore;
import com.gengzp.common.context.MyBatisContextHolder;
import com.gengzp.common.model.generic.resp.Result;
import com.gengzp.common.model.log.req.CreateOperationLogReq;
import com.gengzp.common.model.log.req.CreateTraceLogReq;
import com.gengzp.common.model.log.req.PageOperationLogReq;
import com.gengzp.common.model.log.req.SearchTraceLogReq;
import com.gengzp.common.model.log.resp.ListTraceLogResp;
import com.gengzp.common.model.log.resp.PageOperationLogResp;
import com.gengzp.common.model.pagination.resp.PaginationResponse;
import com.gengzp.permission.annotation.AuthIgnore;
import com.gengzp.permission.annotation.CheckPermission;
import com.gengzp.system.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName SysLogController
 * @Description 系统操作日志控制层
 * @Author gengzp
 * @Date 2025/9/28 14:42
 */
@RestController
@RequestMapping("/log")
@Api(tags = "系统操作日志管理模块")
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;

    /**
     * 创建系统链路跟踪日志
     */
    @AuthIgnore
    @OperationLogIgnore
    @PostMapping("/trace/create")
    @ApiOperation(value = "创建系统链路跟踪日志")
    public Result<Void> createSysOperateLog(@Valid @RequestBody CreateTraceLogReq req) {
        // 不打印sql日志
        MyBatisContextHolder.setPrintSqlLogFlag(false);
        sysLogService.createSysTraceLog(req);
        return Result.getSuccessResult();
    }

    /**
     * 根据 链路追踪id 查询系统日志
     */
    @OperationLogIgnore
    @PostMapping("/trace/page")
    @ApiOperation(value = "根据链路追踪id获取系统操作日志")
    @CheckPermission("log:read")
    public Result<List<ListTraceLogResp>> listTraceLogs(@Valid @RequestBody SearchTraceLogReq req) {
        return Result.getSuccessResult(sysLogService.listTraceLogs(req));
    }

    /**
     * 创建系统操作日志
     */
    @AuthIgnore
    @OperationLogIgnore
    @PostMapping("/operation/create")
    @ApiOperation(value = "创建系统操作日志")
    public Result<Void> createSysOperationLog(@Valid @RequestBody CreateOperationLogReq req) {
        // 不打印sql日志
        MyBatisContextHolder.setPrintSqlLogFlag(false);
        sysLogService.createSysOperationLog(req);
        return Result.getSuccessResult();
    }

    /**
     * 分页查询系统操作日志
     */
    @OperationLogIgnore
    @PostMapping("/operation/page")
    @ApiOperation(value = "分页查询系统操作日志")
    @CheckPermission("log:read")
    public Result<PaginationResponse<PageOperationLogResp>> pageQueryOperationLogs(@Valid @RequestBody PageOperationLogReq req) {
        return Result.getSuccessResult(sysLogService.pageQueryOperationLogs(req));
    }

    /**
     * 清理超时的链路追踪日志 (14天)
     * 该方法用于定时任务调用
     */
    @AuthIgnore
    @ApiOperation(value = "清理超时的链路追踪日志定时任务")
    @PostMapping("/trace/outdated_clear")
    public Result<Void> clearOutdatedTraceLogs() {
        sysLogService.clearOutdatedTraceLogs();
        return Result.getSuccessResult();
    }

}
